<?php
/**
 * @Author: Wang chunsheng
 * @Date:   2020-04-29 11:18:15
 * @Last Modified by:   Wang chunsheng  email:2192138785@qq.com
 * @Last Modified time: 2020-05-29 22:15:17
 */

namespace common\addons\diandi_shop\api;

use Yii;
use api\controllers\AController;
use common\addons\diandi_shop\services\OrderService;
use common\helpers\ResultHelper;

class OrderController extends AController
{
    public $modelClass = '\common\models\DdGoods';

    public function actionSearch()
    {
        return [
            'error_code' => 20,
            'res_msg' => 'ok',
        ];
    }

    /**
     * @SWG\Post(path="/diandi_shop/order/createorder",
     *     tags={"订单"},
     *     summary="立即购买.",
     *     @SWG\Response(
     *         response = 200,
     *         description = "首页",
     *     ),
     *     @SWG\Parameter(
     *      name="access-token",
     *      type="string",
     *      in="query",
     *      required=true
     *     ),
     *     @SWG\Parameter(
       *          in="formData",
     *          name="goods",
     *          type="string",
     *          description="商品数据",
     *          required=true,
     *   ),
     *     @SWG\Parameter(
       *          in="formData",
     *          name="total_price",
     *          type="integer",
     *          description="订单总额",
     *          required=true,
     *   ),
     *     @SWG\Parameter(
       *          in="formData",
     *          name="express_price",
     *          type="integer",
     *          description="运费",
     *          required=true,
     *   ),
     *     @SWG\Parameter(
       *          in="formData",
     *          name="express_type",
     *          type="integer",
     *          enum={0,1},
     *          description="收货方式0配送点配送1收货地址收货",
     *          required=true,
     *   ),
     *     @SWG\Parameter(
       *          in="formData",
     *          name="address_id",
     *          type="integer",
     *          description="配送区域id",
     *          required=true,
     *   ),

     *     @SWG\Parameter(
       *          in="formData",
     *          name="name",
     *          type="string",
     *          description="收货人",
     *          required=false,
     *   ),
     *   @SWG\Parameter(
       *          in="formData",
     *          name="phone",
     *          type="integer",
     *          description="手机号",
     *          required=true,
     *   ),
     *   @SWG\Parameter(
       *          in="formData",
     *          name="detail",
     *          type="string",
     *          description="详细的收货地址",
     *          required=true,
     *   ),
     *   @SWG\Parameter(
       *          in="formData",
     *          name="remark",
     *          type="string",
     *          description="订单备注",
     *          required=true,
     *   ),
     *   @SWG\Parameter(
       *          in="formData",
     *          name="delivery_time",
     *          type="string",
     *          description="配送时间",
     *          required=true,
     *   ),
     * )
     */
    public function actionCreateorder()
    {
        $data = Yii::$app->request->post();
        $total_price = $data['total_price'];
        $express_price = $data['express_price'];
        $express_type = $data['express_type'];
        $remark = $data['remark'];

        $name = '';
        $phone = 0;
        $detail = '';

        if ($data['express_type'] == 1) {
            if (empty($data['address_id']) || $data['address_id'] == 'undefined') {
                return ResultHelper::json(401, '请选择自提点', []);
            }
            if (empty($data['name'])) {
                return ResultHelper::json(401, '请输入收货人姓名', []);
            }
            if (empty($data['phone'])) {
                return ResultHelper::json(401, '请输入收货人手机号', []);
            }

            // if (empty($data['detail'])) {
            //   return ResultHelper::json(401, '请输入收货详细地址具体到楼层房间号', []);
            // }

            $name = $data['name'];
            $phone = $data['phone'];
            $delivery_time = $data['delivery_time'];
        } else {
            if (empty($data['address_id']) || $data['address_id'] == 'undefined') {
                return ResultHelper::json(401, '请选择收货地址', []);
            }
        }

        $address_id = $data['address_id'];

        $user_id = Yii::$app->user->identity->member_id;
        $goods = json_decode($data['goods'], true);
        $orderInfo = OrderService::createOrder($user_id, $goods['goods'], $total_price, $express_price, $express_type, $address_id, $remark, $name, $phone, $delivery_time);

        return ResultHelper::json(200, '创建订单成功', $orderInfo);
    }

    /**
     * @SWG\Post(path="/diandi_shop/order/cart",
     *     tags={"订单"},
     *     summary="购物车购买.",
     *     @SWG\Response(
     *         response = 200,
     *         description = "首页",
     *     ),
     *     @SWG\Parameter(
         *     in="formData",
         *     name="access-token",
         *     type="string",
         *     description="用户秘钥",
         *     required=true,
         *   ),
     * )
     */
    public function actionCart()
    {
    }

    /**
     * @SWG\Post(path="/diandi_shop/order/confirm",
     *     tags={"订单"},
     *     summary="订单操作",
     *     @SWG\Response(
     *         response = 200,
     *         description = "订单操作",
     *     ),
     *     @SWG\Parameter(
     *        in="formData",
     *        name="order_id",
     *        type="integer",
     *        description="订单id",
     *        required=true,
     *   ),
     *     @SWG\Parameter(
     *        in="formData",
     *        name="ctype",
     *        type="string",
     *        description="操作类型",
     *        enum={"qxdd","qrfh","qrsh","scdd"},
     *        required=true,
     *   ),
     *     @SWG\Parameter(
     *        in="query",
     *        name="access-token",
     *        type="string",
     *        description="用户秘钥",
     *        required=true,
     *   ),
     * )
     */
    public function actionConfirm()
    {
        $order_id = Yii::$app->request->post('order_id');
        $ctype = Yii::$app->request->post('ctype');

        return OrderService::confirmOrder($order_id, $ctype);
    }

    /**
     * @SWG\Post(path="/diandi_shop/order/list",
     *     tags={"订单"},
     *     summary="订单列表.",
     *     @SWG\Response(
     *         response = 200,
     *         description = "首页",
     *     ),
     *     @SWG\Parameter(
     *        in="query",
     *        name="access-token",
     *        type="string",
     *        description="用户秘钥",
     *        required=true,
     *   ),
     *     @SWG\Parameter(
     *        in="formData",
     *        name="order_status",
     *        type="integer",
     *        description="订单状态,全部不传递参数，具体的传递参数",
     *        required=false,
     *   ),
     *     @SWG\Parameter(
     *        in="query",
     *        name="page",
     *        type="integer",
     *        description="页码",
     *        required=true,
     *   ),
     *   @SWG\Parameter(
     *        in="formData",
     *        name="pageSize",
     *        type="integer",
     *        description="显示数量",
     *        required=true,
     *   ),

     * )
     */
    public function actionList()
    {
        $user_id = Yii::$app->user->identity->member_id;
        $pageSize = Yii::$app->request->post('pageSize');
        $order_status = Yii::$app->request->post('order_status');
        $order_status = $order_status == -1 ? '' : $order_status;
        $list = OrderService::list($user_id, $order_status, $pageSize);

        return ResultHelper::json(200, '获取成功', $list);
    }

    /**
     * @SWG\Post(path="/diandi_shop/order/detail",
     *     tags={"订单"},
     *     summary="订单详情.",
     *     @SWG\Response(
     *         response = 200,
     *         description = "订单详情",
     *     ),
     *     @SWG\Parameter(
     *        in="query",
     *        name="access-token",
     *        type="string",
     *        description="用户秘钥",
     *        required=true,
     *   ),
     *     @SWG\Parameter(
     *        in="formData",
     *        name="order_id",
     *        type="integer",
     *        description="订单ID",
     *        required=true,
     *   ),
     * )
     */
    public function actionDetail()
    {
        $order_id = Yii::$app->request->post('order_id');
        $user_id = Yii::$app->user->identity->member_id;
        $res = OrderService::detail($order_id);

        return ResultHelper::json(200, '获取成功', $res);
    }

    /**
     * @SWG\Post(path="/diandi_shop/order/delivery",
     *     tags={"订单"},
     *     summary="确认收货.",
     *     @SWG\Response(
     *         response = 200,
     *         description = "首页",
     *     ),
     *     @SWG\Parameter(
         *     in="formData",
         *     name="access-token",
         *     type="string",
         *     description="用户秘钥",
         *     required=true,
         *   ),
     * )
     */
    public function actionDelivery()
    {
    }

    /**
     * @SWG\Post(path="/diandi_shop/order/logistics",
     *     tags={"订单"},
     *     summary="物流跟踪.",
     *     @SWG\Response(
     *         response = 200,
     *         description = "首页",
     *     ),
     *     @SWG\Parameter(
         *     in="formData",
         *     name="access-token",
         *     type="string",
         *     description="用户秘钥",
         *     required=true,
         *   ),
     * )
     */
    public function actionLogistics()
    {
    }

    /**
     * @SWG\Post(path="/diandi_shop/order/comment",
     *     tags={"订单"},
     *     summary="评价商品.",
     *     @SWG\Response(
     *         response = 200,
     *         description = "首页",
     *     ),
     *     @SWG\Parameter(
         *     in="formData",
         *     name="access-token",
         *     type="string",
         *     description="用户秘钥",
         *     required=true,
         *   ),
     * )
     */
    public function actionComment()
    {
    }
}
